设备显示信息 | 您所在的位置:网站首页 › android 获取当前屏幕尺寸 › 设备显示信息 |
设备显示信息
项目05/14/2024
浏览示例 本文介绍如何使用 .NET Multi-platform App UI (.NET MAUI) IDeviceDisplay 接口读取有关设备的屏幕指标的信息。 此接口可用于请求屏幕在应用运行时保持清醒状态。 IDeviceDisplay 接口的默认实现通过 DeviceDisplay.Current 属性提供。 IDeviceDisplay 接口和 DeviceDisplay 类都包含在 Microsoft.Maui.Devices 命名空间中。 主显示器信息MainDisplayInfo 属性返回有关屏幕和方向的信息。 下面的代码示例使用页上的 Loaded 事件读取有关当前屏幕的信息: private void ReadDeviceDisplay() { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.AppendLine($"Pixel width: {DeviceDisplay.Current.MainDisplayInfo.Width} / Pixel Height: {DeviceDisplay.Current.MainDisplayInfo.Height}"); sb.AppendLine($"Density: {DeviceDisplay.Current.MainDisplayInfo.Density}"); sb.AppendLine($"Orientation: {DeviceDisplay.Current.MainDisplayInfo.Orientation}"); sb.AppendLine($"Rotation: {DeviceDisplay.Current.MainDisplayInfo.Rotation}"); sb.AppendLine($"Refresh Rate: {DeviceDisplay.Current.MainDisplayInfo.RefreshRate}"); DisplayDetailsLabel.Text = sb.ToString(); }IDeviceDisplay 接口还提供当任何屏幕指标发生更改时引发的 MainDisplayInfoChanged 事件,例如设备方向从 DisplayOrientation.Landscape 更改为 DisplayOrientation.Portrait 时。 保持屏幕打开还可以通过将 KeepScreenOn 属性设置为 true 阻止设备锁定或屏幕关闭。 每当按下开关控件时,以下代码示例会切换屏幕锁: private void AlwaysOnSwitch_Toggled(object sender, ToggledEventArgs e) => DeviceDisplay.Current.KeepScreenOn = AlwaysOnSwitch.IsToggled; 平台差异本部分利用设备显示器介绍特定于平台的差异。 Android iOS/Mac Catalyst Windows无平台差异。 必须在 UI 线程上访问 DeviceDisplay.Current,否则将引发异常。 可以使用 MainThread.BeginInvokeOnMainThread 方法在 UI 线程上运行该代码。无平台差异。 |
CopyRight 2018-2019 实验室设备网 版权所有 |